<template>
  <div class="dialog">
    <el-dialog width="45%" title="修改密码" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="当前密码：" prop="oldPassword">
          <el-col :span="18">
            <el-input v-model="form.oldPassword" placeholder="请输入" show-password clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPassword">
          <el-col :span="18">
            <el-input v-model="form.newPassword" placeholder="请输入" show-password clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="确认密码：" prop="newPasswordConfirm">
          <el-col :span="18">
            <el-input v-model="form.newPasswordConfirm" placeholder="请输入" show-password clearable />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'Password',
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value && value !== this.form.newPassword) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }

    return {
      form: {
        oldPassword: '',
        newPassword: '',
        newPasswordConfirm: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入密码', trigger: ['blur'] }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: ['blur'] }
        ],
        newPasswordConfirm: [
          { required: true, message: '请再次输入新密码', trigger: ['blur'] },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },

  methods: {

    // 取消
    close() {
      this.$emit('close')
    },

    // 确认
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('confirm', this.form)
        } else {
          return false
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
